package ch19 {
	/**
	 * 声音
	 * @author jianping.shenjp
	 */
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.media.Sound;
	import flash.net.URLRequest;
	import utils.Ball;

	public class SoundEvents extends Sprite {
		private var ball:Ball;
		private var vx:Number;
		private var vy:Number;
		private var bounce:Number = -0.7;
		private var boing:Sound;

		public function SoundEvents(){
			init();
		}

		private function init():void {
			boing = new Sound(new URLRequest("../assets/boing.mp3"));
			boing.addEventListener(Event.COMPLETE, completeHandler);
			boing.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			ball = new Ball();
			ball.x = stage.stageWidth / 2;
			ball.y = stage.stageHeight / 2;
			vx = Math.random() * 10 - 5;
			vy = -10;
			addChild(ball);
		}

		private function completeHandler(e:Event):void {
			trace("completeHandler");
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}

		private function ioErrorHandler(e:IOErrorEvent):void {
			trace("ioErrorHandler!");
		}

		private function onEnterFrame(event:Event):void {
			ball.x += vx;
			ball.y += vy;
			var left:Number = 0;
			var right:Number = stage.stageWidth;
			var top:Number = 0;
			var bottom:Number = stage.stageHeight;
			if (ball.x + ball.radius > right){
				boing.play();
				ball.x = right - ball.radius;
				vx *= bounce;
			} else if (ball.x - ball.radius < left){
				boing.play();
				ball.x = left + ball.radius;
				vx *= bounce;
			}
			if (ball.y + ball.radius > bottom){
				boing.play();
				ball.y = bottom - ball.radius;
				vy *= bounce;
			} else if (ball.y - ball.radius < top){
				boing.play();
				ball.y = top + ball.radius;
				vy *= bounce;
			}
		}
	}

}